//
//  MeatMarketListVIewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/15.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatMainListViewController.h"

#import "MeatBuyViewController.h"
#import "MeatMineViewController.h"
#import "MeatHotWordViewController.h"
#import "MeatCityViewController.h"
#import "MeatMapViewController.h"

#import "MeatMainHeader.h"


@interface MeatMainListViewController ()

@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) NSArray *controls;

@property (nonatomic, strong) MeatMainHeader *header;
@property (nonatomic, strong) MeatMarketApi *meatApi;
@property (nonatomic, strong) BuyMeatPartListModel *menuListModel;

@end

@implementation MeatMainListViewController

#pragma mark - view life cycle

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self obtainLocations];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initData];
    [self setUpView];
    [self addNotifications];
}

- (void)obtainLocations
{
    LocationManager *locationManager = [LocationManager sharedManager];
    
     @weakify(self);
    [locationManager startLocationCompleteBlock:^(double latitude, double longitude) {
        @strongify(self);
        [kUserDefaults setDouble:latitude forKey:kShopLat];
        [kUserDefaults setDouble:longitude forKey:kShopLon];
        [kUserDefaults synchronize];
    }];

   
    locationManager.locationNameBlock = ^(NSString *location, double latitude, double longitude) {
        @strongify(self);
        self.header.location = location;
    };
    
}

#pragma mark - networking methods

- (void)initData{
    
    [self requestBuyMeatPartDataWithType:@1];
}

- (void)requestBuyMeatPartDataWithType:(NSNumber *)type{
    
    @weakify(self);
    [[self.meatApi getBuyMeatPartListDataWithType:type] subscribeNext:^(BuyMeatPartListModel * _Nullable menuListModel) {
        @strongify(self);
        
        self.menuListModel = [[BuyMeatPartListModel alloc] init];
        self.menuListModel = menuListModel;
        self.header.keys = menuListModel.keyWord;
        
    }error:^(NSError * _Nullable error) {
        
        @strongify(self);
        [self handlingError:error];
    }];
}


#pragma mark - init method

- (void)setUpView {
    [self setupNavBar];
    [self setupChildViewControllers];
    [self setupChildSetting];
}

- (void)setupNavBar{
    [self.view addSubview:self.header];
}

- (void)setupChildViewControllers{

    kWeakSelf(self);
    [self.controls enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        UIViewController *vc = (UIViewController *)obj;
        vc.title = weakself.titles[idx];
        [weakself addChildViewController:vc];
    }];
}


- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    kWeakSelf(self);
    [self setUpContentViewFrame:^(UIView *contentView) {
        CGFloat contentX = 0;
        CGFloat contentH = KScreenHeight - weakself.header.height - TJ_TabBarHeight;
        contentView.frame = CGRectMake(contentX, weakself.header.height, KScreenWidth, contentH);
    }];
}


- (void)setupChildSetting
{
    kWeakSelf(self);
    [self setUpContentViewFrame:^(UIView *contentView) {
        CGFloat contentX = 0;
        CGFloat contentH = KScreenHeight - weakself.header.height - TJ_TabBarHeight ;
        contentView.frame = CGRectMake(contentX, weakself.header.height, KScreenWidth, contentH);
    }];
    
    [self setUpTitleEffect:^(UIColor *__autoreleasing *titleScrollViewColor, UIColor *__autoreleasing *norColor, UIColor *__autoreleasing *selColor, UIFont *__autoreleasing *titleFont, CGFloat *titleHeight,CGFloat *titleWidth) {
        
        // 设置标题字体
        *titleFont = mFont(15);
        *norColor = UIColorFromRGB(0x333333);
        *selColor = mMainColor;
        *titleHeight = 42;
        *titleScrollViewColor = KWhiteColor;
    }];
    
    // 推荐方式（设置下标）
    [self setUpUnderLineEffect:^(BOOL *isUnderLineDelayScroll, CGFloat *underLineH, UIColor *__autoreleasing *underLineColor,BOOL *isUnderLineEqualTitleWidth) {
        // 标题填充模式
        *underLineColor = mMainColor;
        *isUnderLineEqualTitleWidth = YES;
        *underLineH = 3;
    }];
    
}


#pragma mark - setter and getter

- (NSArray *)titles{
    if (!_titles) {
        _titles = @[@"卖肉",@"同城掌柜",@"我的肉铺"];
    }
    return _titles;
}


- (MeatMarketApi *)meatApi{
    
    if(!_meatApi){
        _meatApi = [[MeatMarketApi alloc]init];
    }
    return _meatApi;
}


- (NSArray *)controls
{
    if (!_controls) {
        
        MeatBuyViewController *oneVC = [[MeatBuyViewController alloc] init];
        MeatCityViewController *twoVC = [[MeatCityViewController alloc]init];
        MeatMineViewController *threeVC = [[MeatMineViewController alloc]init];
        _controls = @[oneVC,twoVC,threeVC];
    }
    return _controls;
}


- (MeatMainHeader *)header{
    
    if (!_header) {
        _header = [[MeatMainHeader alloc] init];
        
        @weakify(self);
        _header.dealSearchBlock = ^{
            @strongify(self);
            //点击搜索按钮
            MeatHotWordViewController *vc = [[MeatHotWordViewController alloc]init];
            vc.hotWords = self.menuListModel.recomWord;
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
            [self presentViewController:nav animated:YES completion:nil];

        };
        
        
        _header.dealKeyBoardBlock = ^(NSInteger params) {
            @strongify(self);
            NSLog(@"______________   %ld", params);
        };
        
        
        
        _header.dealLocationBlock = ^{
            @strongify(self);
            //点击搜索按钮
            MeatMapViewController *vc = [[MeatMapViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
            
        };
    }
    return _header;
}


#pragma mark - other methods

- (void)addNotifications{
    
    [kNotificationCenter addObserver:self selector:@selector(initData) name:kMeatMarketMainHeaderNotification object:nil];
    [kNotificationCenter addObserver:self selector:@selector(hideBox) name:@"YZDisplayViewClickOrScrollDidFinshNote" object:nil];
}

- (void)removeNotifications{
    
    [kNotificationCenter removeObserver:self];
}

- (void)hideBox{
    [kNotificationCenter postNotificationName:KMeatMarketCleanScreeningNotification object:nil];
}

- (void)dealloc{
    
    [self removeNotifications];
}




@end
